#include "algimpurity.h"

AlgImpurity::AlgImpurity()
{

}
//  说明 num 是点击执行检测 采集图片的张数，当张数大于2 才开始调用该函数
// ho_B 是后一张采集的图像
//ho_A 是前一张采集的图像  例如 B是第三张 那么A是第二张图片
//parameter[10]  就是那几天要你设置的几个参数，程序界面可以调节的

// 点击执行程序后，图像界面显 ho_B 这个数据
// 如果整个函数返回1 NG，如果0 OK

int AlgImpurity::detect(HObject  ho_A, HObject   ho_B, int num, int ccd_num)
{
    //*参数1  检查1的阈值
    //*参数2 图像帧数
    //*参数3  检查2的阈值
    //**参数4  检测1面积
    //**参数5  检测2面积
    CopyImage(ho_A, &g_ho_ImageResult);
    if (num < 2)
    {
        return 0;

    }
    // return 0;

     if (ccd_num == 3|| ccd_num ==2)
     {
      

        // return 0;
     }
     //  if (ccd_num==3)
     //  {
     //    

     //      int b = num;
     //      int a = g_ccd1;

           //QString path = QString("%1%2%3%4.bmp").arg("d://1//").arg(a).arg("_0_").arg(b);
           //HalconCpp::WriteImage(ho_A, "bmp", 0, path.toStdString().c_str());


           ////QString path1 = QString("%1%2%3.bmp").arg("d://1//").arg(g_ccd1).arg("_1");
           ////HalconCpp::WriteImage(ho_B, "bmp", 0, path1.toStdString().c_str());
     //     
     //  }
    if (ccd_num == 1)
    {
        QString path = QString("%1%2%3.bmp").arg("d://1//").arg(g_ccd1).arg("_0");
        //  HalconCpp::WriteImage(ho_A, "bmp", 0, path.toStdString().c_str());
        //QString path1 = QString("%1%2%3.bmp").arg("d://1//").arg(g_ccd1).arg("_1");
        //HalconCpp::WriteImage(ho_A, "bmp", 0, path.toStdString().c_str());
     //  HalconCpp::WriteImage(ho_B, "bmp", 0, path1.toStdString().c_str());
        //g_ccd1++;
    }
    g_ccd1++;

    HTuple  hv_parameter0, hv_parameter1, hv_parameter2;
    HTuple  hv_parameter3, hv_parameter4, hv_parameter5, hv_parameter6, hv_parameter7;
    /*
    hv_parameter0 = m_part.parameter1;  //液位灵敏度
    hv_parameter1 = m_part.parameter2;// 检测1阈值
    hv_parameter2 = m_part.parameter3;//检测1 灵敏度
    hv_parameter3 = m_part.parameter4;//检测2 阈值
    hv_parameter4 = m_part.parameter5;//检测2 灵敏度
    hv_parameter5 = m_part.parameter6;////液位位置
    hv_parameter6 = m_part.parameter7; //检测张数
    */

    hv_parameter0 = 30;
    hv_parameter1 = 10;
    hv_parameter2 = 20;
    hv_parameter3 = 15;
    hv_parameter4 = 30;
    hv_parameter5 = 30;
    hv_parameter6 = 10;


    HObject ho_Region1, ho_RegionFillUp;
    HObject  ho_RegionClipped, ho_Rectangle, ho_Rectangle2, ho_Rectangle3;
    HObject  ho_ImageResult4, ho_ImageAbsDiff, ho_Region, ho_ROI_0;
    HObject  ho_RegionIntersection, ho_ConnectedRegions, ho_SelectedRegions;
    HObject  ho_RegionDilation, ho_RegionDilation1, ho_RegionDifference;
    HObject  ho_ImageResult1, ho_ImageResult2, ho_ImageResult3, ho_ImageResult11, ho_ImageResult12, ho_ImageResult13;
    HObject  ho_MultiChannelImage, ho_Image1;

    // Local control variables

    HTuple  hv_img_num, hv_Row1, hv_Column1, hv_Row2, hv_Column2;
    HTuple  hv_Abs, hv_Number, hv_Width, hv_Height;

    HTuple  hv_HomMat2DIdentity, hv_HomMat2DTranslate, hv_Area, hv_Row, hv_Column;

    HObject  ho_ImageAffineTrans, ho_ImageAbsDiff1, ho_Region3;
    HObject  ho_Region2, ho_RegionDifference1;


	GetImageSize(ho_B, &hv_Width, &hv_Height);

	GenImage1(&ho_Image1, "byte", hv_Width, hv_Height, 0);
    ///空位
    
	Threshold(ho_A, &ho_Region3, 150, 255);
    GenRectangle1(&ho_Rectangle, 0, 0, 600, hv_Width);
	Intersection(ho_Rectangle, ho_Region3, &ho_Region3);
	AreaCenter(ho_Region3, &hv_Area, &hv_Row, &hv_Column);

	if (0 != (hv_Area < 10000))
	{
        return  0;

	}

    





    
    ////液位
    if (ccd_num == 3 && num == 19)
    {

        HObject ho_RegionErosion;

        HTuple  hv_Row11, hv_Column11, hv_Row21, hv_Column21;

        Threshold(ho_B, &ho_Region, 50, 255);

        FillUp(ho_Region, &ho_RegionFillUp);

        ErosionRectangle1(ho_RegionFillUp, &ho_RegionErosion, 11, 11);
        Connection(ho_RegionErosion, &ho_ConnectedRegions);
        SelectShape(ho_ConnectedRegions, &ho_SelectedRegions, "area", "and", 10000, 999999);
        SmallestRectangle1(ho_SelectedRegions, &hv_Row1, &hv_Column1, &hv_Row2, &hv_Column2);
        if (0 != (hv_Row1 < 5) || 0 != ((hv_Column2 - hv_Column1)) < 100)
        {
            return 0;
        }

        GenRectangle1(&ho_Rectangle, hv_Row1, hv_Column1, hv_Row2, hv_Column1 + 5);
        Intersection(ho_SelectedRegions, ho_Rectangle, &ho_RegionIntersection);
        SmallestRectangle1(ho_RegionIntersection, &hv_Row11, &hv_Column11, &hv_Row21, &hv_Column21);

        if (0 != (hv_Row21 < 5))
        {
            return 0;
        }

        GenRectangle1(&ho_Rectangle, hv_Row21, hv_Column1, hv_Row21 + 20, hv_Column2);


        GenRectangle1(&ho_Rectangle2, hv_parameter5, hv_Column1, hv_parameter5 + 20, hv_Column2);
        GenRectangle1(&ho_Rectangle3, hv_parameter5 + 80, hv_Column1, hv_parameter5 + 100, hv_Column2);


        PaintRegion(ho_Rectangle, ho_B, &ho_ImageResult11, 255, "fill");
        PaintRegion(ho_Rectangle2, ho_B, &ho_ImageResult12, 255, "fill");
        PaintRegion(ho_Rectangle3, ho_B, &ho_ImageResult13, 255, "fill");
        TupleFabs(hv_Row21 - hv_parameter5, &hv_Abs);
        Compose3(ho_ImageResult11, ho_ImageResult12, ho_ImageResult13, &ho_MultiChannelImage);
        CopyImage(ho_MultiChannelImage, &g_ho_ImageResult);
        // WriteImage(ho_MultiChannelImage, "bmp", 0, "D:/1111.bmp");
        if (0 != (hv_Abs > hv_parameter0))
        {
               return 1;

        }
        return 0;
    }



    AbsDiffImage(ho_A, ho_B, &ho_ImageAbsDiff, 1);
    if (num < hv_parameter6)
    {
        Threshold(ho_ImageAbsDiff, &ho_Region, hv_parameter1, 255);
        GenRectangle1(&ho_ROI_0, 500, 0, 1200, 800);
        Intersection(ho_ROI_0, ho_Region, &ho_RegionIntersection);

        HomMat2dIdentity(&hv_HomMat2DIdentity);
        HomMat2dTranslate(hv_HomMat2DIdentity, 3, 3, &hv_HomMat2DTranslate);
        AffineTransImage(ho_A, &ho_ImageAffineTrans, hv_HomMat2DTranslate, "constant",
            "false");
        AbsDiffImage(ho_ImageAffineTrans, ho_A, &ho_ImageAbsDiff1, 1);
        Threshold(ho_ImageAbsDiff1, &ho_Region1, 30, 255);
        HomMat2dTranslate(hv_HomMat2DIdentity, -3, -3, &hv_HomMat2DTranslate);
        AffineTransImage(ho_B, &ho_ImageAffineTrans, hv_HomMat2DTranslate, "constant",
            "false");
        AbsDiffImage(ho_ImageAffineTrans, ho_B, &ho_ImageAbsDiff1, 1);
        Threshold(ho_ImageAbsDiff1, &ho_Region2, 30, 255);
        Difference(ho_RegionIntersection, ho_Region1, &ho_RegionDifference1);
        Difference(ho_RegionDifference1, ho_Region2, &ho_RegionIntersection);


        FillUp(ho_RegionIntersection, &ho_RegionIntersection);
        Connection(ho_RegionIntersection, &ho_ConnectedRegions);
        SelectShape(ho_ConnectedRegions, &ho_SelectedRegions, "area", "and", hv_parameter2,
            9999999);
    }
    if (num > hv_parameter6 - 1)
    {
        Threshold(ho_ImageAbsDiff, &ho_Region, hv_parameter3, 255);
        GenRectangle1(&ho_ROI_0, 400, 0, 1200, 600);
        Intersection(ho_ROI_0, ho_Region, &ho_RegionIntersection);


        HomMat2dIdentity(&hv_HomMat2DIdentity);
        HomMat2dTranslate(hv_HomMat2DIdentity, 2, 2, &hv_HomMat2DTranslate);
        AffineTransImage(ho_A, &ho_ImageAffineTrans, hv_HomMat2DTranslate, "constant",
            "false");
        AbsDiffImage(ho_ImageAffineTrans, ho_A, &ho_ImageAbsDiff1, 1);
        Threshold(ho_ImageAbsDiff1, &ho_Region1, 30, 255);


        HomMat2dTranslate(hv_HomMat2DIdentity, -2, -2, &hv_HomMat2DTranslate);
        AffineTransImage(ho_B, &ho_ImageAffineTrans, hv_HomMat2DTranslate, "constant",
            "false");
        AbsDiffImage(ho_ImageAffineTrans, ho_B, &ho_ImageAbsDiff1, 1);
        Threshold(ho_ImageAbsDiff1, &ho_Region2, 30, 255);

        Difference(ho_RegionIntersection, ho_Region1, &ho_RegionDifference1);
        Difference(ho_RegionDifference1, ho_Region2, &ho_RegionIntersection);



		FillUp(ho_RegionIntersection, &ho_RegionIntersection);


        Connection(ho_RegionIntersection, &ho_ConnectedRegions);
        SelectShape(ho_ConnectedRegions, &ho_SelectedRegions, "area", "and", hv_parameter4,
            9999999);
    }
    CountObj(ho_SelectedRegions, &hv_Number);

    if (0 != (hv_Number > 0))
    {

        HTuple hv_Index, hv_Row12, hv_Row22, hv_Column12, hv_Column22;
        HTuple end_val28 = hv_Number;
        HObject ho_SelectedRegions1, ho_ObjectSelected, ho_ConnectedRegions2, ho_SelectedRegions2;
        HTuple step_val28 = 1;
        for (hv_Index = 1; hv_Index.Continue(end_val28, step_val28); hv_Index += step_val28)
        {
            SelectObj(ho_SelectedRegions, &ho_ObjectSelected, hv_Index);
            SmallestRectangle1(ho_ObjectSelected, &hv_Row12, &hv_Column12, &hv_Row22,
                &hv_Column22);
            if (0 != ((hv_Column22 - hv_Column12) > (1.5 * (hv_Row22 - hv_Row12))))
            {
                PaintRegion(ho_ObjectSelected, ho_Image1, &ho_Image1, 255, "fill");
            }
        }
        Threshold(ho_Image1, &ho_Region1, 128, 255);
        Connection(ho_Region1, &ho_ConnectedRegions2);
        SelectShape(ho_ConnectedRegions2, &ho_SelectedRegions, "area", "and", 30, 9999999);
        CountObj(ho_SelectedRegions, &hv_Number);
    }

    if (0 != (hv_Number > 0))
    {
        ShapeTrans(ho_SelectedRegions, &ho_SelectedRegions, "rectangle1");
        DilationRectangle1(ho_SelectedRegions, &ho_RegionDilation, 50, 50);
        DilationRectangle1(ho_SelectedRegions, &ho_RegionDilation1, 35, 35);
        Difference(ho_RegionDilation, ho_RegionDilation1, &ho_RegionDifference);
        PaintRegion(ho_RegionDifference, ho_B, &ho_ImageResult1, 255, "fill");
        PaintRegion(ho_RegionDifference, ho_B, &ho_ImageResult2, 0, "fill");
        PaintRegion(ho_RegionDifference, ho_B, &ho_ImageResult3, 0, "fill");
        Compose3(ho_ImageResult1, ho_ImageResult2, ho_ImageResult3, &ho_MultiChannelImage);
        CopyImage(ho_MultiChannelImage, &g_ho_ImageResult);



        int a = g_ccd1;

        if (ccd_num == 1)
        {
            QString path = QString("%1%2%3.bmp").arg("d://1//").arg(a).arg("_0_");
            HalconCpp::WriteImage(ho_A, "bmp", 0, path.toStdString().c_str());


            QString path1 = QString("%1%2%3.bmp").arg("d://1//").arg(g_ccd1).arg("_1_");
            HalconCpp::WriteImage(ho_B, "bmp", 0, path1.toStdString().c_str());

			QString path2 = QString("%1%2%3.bmp").arg("d://1//").arg(g_ccd1).arg("_2_");
			HalconCpp::WriteImage(g_ho_ImageResult, "bmp", 0, path2.toStdString().c_str());


        }



		if (ccd_num == 2)
		{
			QString path = QString("%1%2%3.bmp").arg("d://2//").arg(a).arg("_0_");
			HalconCpp::WriteImage(ho_A, "bmp", 0, path.toStdString().c_str());


			QString path1 = QString("%1%2%3.bmp").arg("d://2//").arg(g_ccd1).arg("_1_");
			HalconCpp::WriteImage(ho_B, "bmp", 0, path1.toStdString().c_str());

			QString path2 = QString("%1%2%3.bmp").arg("d://2//").arg(g_ccd1).arg("_2_");
			HalconCpp::WriteImage(g_ho_ImageResult, "bmp", 0, path2.toStdString().c_str());


		}

		if (ccd_num == 3)
		{
			QString path = QString("%1%2%3.bmp").arg("d://3//").arg(a).arg("_0_");
			HalconCpp::WriteImage(ho_A, "bmp", 0, path.toStdString().c_str());


			QString path1 = QString("%1%2%3.bmp").arg("d://3//").arg(g_ccd1).arg("_1_");
			HalconCpp::WriteImage(ho_B, "bmp", 0, path1.toStdString().c_str());

			QString path2 = QString("%1%2%3.bmp").arg("d://3//").arg(g_ccd1).arg("_2_");
			HalconCpp::WriteImage(g_ho_ImageResult, "bmp", 0, path2.toStdString().c_str());


		}

        return 1;
    }

    // }
    return  0;
}



